package function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import job.CarBean;
import org.apache.flink.api.common.functions.MapFunction;

public class CarViewBeanMapFuction implements MapFunction<String, CarBean> {
    @Override
    public CarBean map(String s) throws Exception {
        JSONObject jsonObject = JSON.parseObject(s);
        String vin = jsonObject.getString("vin");
        Long timestamp = jsonObject.getLong("timestamp");
        int type = jsonObject.getIntValue("type");
        double longitude = jsonObject.getDoubleValue("longitude");
        double latitude = jsonObject.getDoubleValue("latitude");
        double speed = jsonObject.getDoubleValue("speed");
        double mileage = jsonObject.getDoubleValue("mileage");
        double batteryLevel = jsonObject.getDoubleValue("batteryLevel");
        double batteryTemp = jsonObject.getDoubleValue("batteryTemp");
        double motorTemp = jsonObject.getDoubleValue("motorTemp");
        String chargingStatus = jsonObject.getString("chargingStatus");
        double energyConsumption = jsonObject.getDoubleValue("energyConsumption");
        return new CarBean(
                vin,
                timestamp,
                type,
                longitude,
                latitude,
                speed,
                mileage,
                batteryLevel,
                batteryTemp,
                motorTemp,
                chargingStatus,
                energyConsumption
        );
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
